package com.song.composejoke.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.song.composejoke.network.NetworkUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class JokeModel implements Serializable {
    /**
     * content : 有一天晚上我俩一起吃西瓜，老大把西瓜籽很整洁的吐在了一张纸上，
     * 过了几天，我从教室回但宿舍看到老大在磕瓜子，
     * 我就问他：老大，你什么时候买的瓜子？
     * 老大说：刚晒好，说着抓了一把要递给我……
     * hashId : bcc5fdc2fb6efc6db33fa242474f108a
     * unixtime : 1418814837
     * updatetime : 2014-12-17 19:13:57
     */

    public String content;
    public String hashId;
    public int unixtime;
    public String updatetime;

    public static void getData(int start, Listener listener) {
        Observable.create(new ObservableOnSubscribe<List<JokeModel>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<JokeModel>> emitter) throws Throwable {
                long time = System.currentTimeMillis() / 1000;
                String json = NetworkUtil.getJSON("https://v.juhe.cn/joke/content/list.php?sort=desc&page=+" + start
                        + "&pagesize=20&time=" + time + "&key=6f5f1dc955e261b2727d419454ad6f21");
                Log.e("JokeModel", json);

                Gson gson = new Gson();
                try {
                    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
                    JsonObject resultObject = (JsonObject) jsonObject.get("result");
                    String data = resultObject.get("data").toString();
                    List<JokeModel> list = gson.fromJson(data, new TypeToken<List<JokeModel>>() {
                    }.getType());
                    emitter.onNext(list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<JokeModel>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull List<JokeModel> list) {
                        listener.onDataLoaded(list);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    public interface Listener {
        void onDataLoaded(List<JokeModel> list);
    }


}
